<template>
  <q-page class="cc-admin">
    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" :class="mobile ? 'flow-table-btn' : ''" separator="cell" :columns="columns" :data="list" row-key="id"
        @request="query" :rows-per-page-options="[10, 20, 50, 100]" :selection="mobile ? 'none' : 'multiple'" :selected.sync="selected" :hide-bottom="false">
        <template v-slot:top="">
          <div class="row no-wrap full-width">
            <q-space />
          </div>
        </template>

        <template #body-cell-index="props">
          <q-td :props="props">{{ props.value }}</q-td>
        </template>
        <template #body-cell-shortScopeText="props">
          <q-td key="shortScopeText" :props="props">
            <div :class="calcRowColor(props.row)">
              {{ props.row.shortScopeText }}
            </div>
          </q-td> </template><template #body-cell-shortMessage="props">
          <q-td key="shortMessage" :props="props">
            <div :class="calcRowColor(props.row)">
              <q-toggle debounce="500" hide-bottom-space v-model="props.row.shortMessage" lazy-rules @input="
                shortMessageInput(props.row.shortMessage, props.row, false)
                " color="primary" trueValue="1" falseValue="0" />
            </div>
          </q-td>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <cc-btn label="编辑" color="primary" icon="edit" @click="edit(props.row)">
              <q-tooltip>编辑</q-tooltip></cc-btn>
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          新增
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="display-item col-6">
              <div class="dynamic-form-item">
                <div v-show="true" class="dynamic-form-item-label" style="width: 100%">
                  <q-icon name="star" color="red" v-show="true" />
                  发送范围
                </div>
                <select-search dense debounce="500" hide-bottom-space outlined :options="shortScope" v-model="form.shortScope" type="select" lazy-rules emit-value
                  :rules="[(v) => !!v || '不可以为空']" map-options @input="shortScopeInput(form.shortScope, form)" />
              </div>
            </div>
            <div class="display-item col-2">
              <div class="dynamic-form-item">
                <div v-show="true" class="dynamic-form-item-label" style="width: 100%">
                  <q-icon name="star" color="red" v-show="false" />
                  是否通知
                </div>
                <q-toggle debounce="500" hide-bottom-space v-model="form.shortMessage" type="toggle" lazy-rules color="primary" trueValue="1" falseValue="0" />
              </div>
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import { requiredTest } from 'boot/inputTest';

export default {
  name: 'MessageNotification',
  mixins: [IndexMixin],
  components: {
    confirm,
  },
  data() {
    return {
      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
        },
        {
          name: 'name',
          align: 'center',
          label: '功能名称',
          field: 'name',
        },
        {
          name: 'shortMessage',
          align: 'center',
          label: '是否通知',
          field: 'shortMessage',
        },
        {
          name: 'shortScopeText',
          align: 'center',
          label: '通知范围',
          field: 'shortScopeText',
        },
        {
          name: 'opt',
          align: 'center',
          label: '操作',
          field: 'id',
        },
      ],
      form: {},
      sysCompany: [],
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/message/notification/list',
        add: '/message/notification/add',
        edit: '/message/notification/edit',
        delete: '/message/notification/delete',
      },
      shortScope: [
        {
          label: '岸基',
          value: '0',
        },
        {
          label: '船员',
          value: '1',
        },
        {
          label: '全员',
          value: '2',
        },
      ],
    };
  },
  methods: {
    requiredTest,
    getDictLabel,
    submit() {
      let q;
      if (this.editType === '新建') {
        q = this.$axios.post(this.url.add, this.form);
      } else {
        q = this.$axios.put(this.url.edit, this.form);
      }
      return q
        .then((r) => {
          this.$info(r.message);
          this.$refs.dialog.hide();
        })
        .catch(() => { })
        .finally(() => {
          this.query();
        });
    },
    sysCompanyInput(v, row, editAfter) {
      // 这里给名称字段赋值
      if (v instanceof Array) {
        const nameArray = [];
        this.form.companyName = '';
        v.forEach((id) => {
          const opt = this.sysCompany.find((o) => o.value === id);
          if (opt) {
            nameArray.push(opt.label);
          }
          this.form.CompanyName = nameArray.join(',');
        });
      } else {
        const opt = this.sysCompany.find((o) => o.value === v);
        this.form.companyName = opt ? opt.label : '';
      }
    },
    shortScopeInput(v, row, editAfter) {
      // 这里给名称字段赋值
      if (v instanceof Array) {
        const nameArray = [];
        this.form.shortScopeText = '';
        v.forEach((id) => {
          const opt = this.shortScope.find((o) => o.value === id);
          if (opt) {
            nameArray.push(opt.label);
          }
          this.form.shortScopeText = nameArray.join(',');
        });
      } else {
        const opt = this.shortScope.find((o) => o.value === v);
        this.form.shortScopeText = opt ? opt.label : '';
      }
    },
    initDict() {
      this.$axios.get('/sys/company/getCompanyName').then((r) => {
        this.sysCompany = r;
      });
    },
    calcRowColor(row) {
      return '';
    },
    shortMessageInput(shortMessage, row) {
      this.$axios.put(this.url.edit, row);
    },
  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus"></style>
